<template>
  <div
    style="margin-top: 15px;min-height: 100%;"
    class="main-div"
  >
    <Spin v-show="isSpinShow" fix size="large"/>
    <!--
   描述：新增、删除和运行按钮
-->
    <div class="filter-container">
      <div class="right-items" style="float: right;">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary"  @click="action('flowImage')">流程图</el-button>
        <el-button class="filter-item" style="margin-left: 10px;" type="danger"  @click="action('invalid')">作废</el-button>
        <el-button class="filter-item" style="margin-left: 10px;" type="success"  @click="action('approvalHistory')">审批历史</el-button>
      </div>
    </div>


    <iframe               ref="iframe"
                          :src="editSrc" id="iframe" scrolling="yes" frameborder="0" style="width:100%;height: 100%;min-height: 700px"
            :height="tableHeight"></iframe>

    <screenfull ref="screenfull" class="right-menu-item hover-effect"/>

    <Modal
      :mask-closable="false"
      v-model="opinion.show"
      title="意见"
      width="85%"
      :transfer="false"
    >
      <Table
        ref="selection"
        highlight-row
        height="450"
        :columns="opinion.tableColumns"
        :data="opinion.tableData"
        style="padding-left: 10px;padding-top: 10px"
      />
      <div slot="footer" />
    </Modal>
    <Modal
      :mask-closable="false"
      v-model="flowImage.show"
      title="流程图"
      width="85%"
      :transfer="false"
    >
      <iframe
        id="flowImageframe"
        width="100%"
        style="min-height: 600px"
        height="100%"
        frameborder="0"
        align="center"
        allowfullscreen="true"
        allow="autoplay"
        :src="flowImage.src"
      />
      <div slot="footer"/>
    </Modal>

  </div>

</template>

<script>
  import {completeAdHoc, completeTask, getNextNode, getTaskBtn, renderByTaskId} from '@/api/analy'
  import {
    addNodeOperate,
    addSignOperate,
    changeActivity,
    getBackNode,
    getBackNodeUser,
    getBiz,
    getOutgoingFlows,
    getTransactionUrge,
    informedTask,
    invalidTask,
    opinionTask,
    saveform,
    transferTask
  } from '@/api/action'
  import userMultipleDialog from '@/views/components/org/userMultipleDialog'
  import userSingleDialog from '@/views/components/org/userSingleDialog'
  import Screenfull from '@/components/Screenfull'

  import Footer from '../../../common/footer'
  import { getToken } from "@/utils/auth"; // get token from cookie

  export default {
    name: 'Task',
    components: {
      Footer,
      userMultipleDialog,
      userSingleDialog,
      Screenfull
    },

    data() {
      return {
        loading: false,
        iframeWin: {},
        fromIndex: false,
        taskId: '',
        bizKey: '',
        processDefinitionId: '',
        processInstanceId: '',
        taskTitle: '',
        tableHeight: '',
        alias: '',
        handlerModel: false,
        handlerSequenceModel: false,
        isSpinShow: true,
        editSrc: '',
        show: false,
        singleShow: false,
        jsonData: {},
        editData: {},
        remoteFuncs: {},
        singleUserData: {},
        dynamicData: {},
        taskBtns: [],
        multiInstanceTaskBtns: [],
        taskSequence: [],
        taskSequenceForm: {
          taskTitle: '',
          userInfo: [],
          taskName: '',
          choose: true,
          taskId: '',
          opinion: '',
          formType: 'inner',
          actionName: '',
          action: '',
          nodeType: '',
          usersIds: [],
          userNames: [],
          nextNode: [],
          chooseNode: '',
          chooseNodeUser: '',
          formData: '',
          nodeIndex: 0,
          priority: '50',

        },
        existData: [],
        nextNode: [],
        sequenceNode: [],
        transferTask:
          {
            usersInfo: {},
            option: '',
            taskId: '',
            show: false,
            model: false
          },
        headMap:
          {
            show: false,
            src: '',
            base: process.env.VUE_APP_BASE_BPM_API + '/head-map/index.html?processefinitionId='
          },
        opinion:
          {
            show: false,
            tableColumns: [
              {
                title: '任务名称',
                key: 'nodeName'
              },
              {
                title: '创建时间',
                key: 'createTime'
              },
              {
                title: '审批时间',
                key: 'completeTime'
              },
              {
                title: '执行人',
                render: (h, params) => {
                  var html = '-'
                  var node = this.opinion.tableData[params.index]
                  if (node.a1FlowTaskTrajectoryEntity) {
                    html = node.a1FlowTaskTrajectoryEntity.userName
                  } else {
                    html = node.auditor
                  }
                  if (!html) {
                    if (node.identityLinks) {
                      html = node.identityLinks
                    }

                  }
                  return h('span', html)
                }
              },
              {
                title: '任务标题',
                render: (h, params) => {
                  var html = '-'
                  var node = this.opinion.tableData[params.index]
                  html = node.a1FlowTaskTrajectoryEntity.title
                  return h('span', html)
                }
              },
              {
                title: '备注/意见',
                render: (h, params) => {
                  var html = '-'
                  var node = this.opinion.tableData[params.index]
                  html = node.a1FlowTaskTrajectoryEntity.opinion
                  return h('span', html)
                }
              },
              {
                title: '状态说明',
                render: (h, params) => {
                  var html = '-'
                  var node = this.opinion.tableData[params.index]
                  html = node.a1FlowTaskTrajectoryEntity.statusVal
                  return h('span', html)
                }
              }

            ],
            tableData: []
          },
        flowImage:
          {
            show: false,
            src: '',
            base: process.env.VUE_APP_BASE_BPM_API + '/view/tab.html?fireHoverAction=true&table=false' + '&instId='
          },
        transactionUrge:
          {
            show: false,
            newActivityId: '',
            operate: 'transactionUrge',
            taskTitle: '',
            taskId: '',
            option: '',
            priority: '50',
            usersInfo: {}
          },
        transactionUrgeData: {
          nodeArray: []
        },
        addNode: {
          show: false,
          nextId: '',
          userTaskName: ''
        },
        signType: [
          {lable: '10', value: '10%'},
          {lable: '20', value: '20%'},
          {lable: '30', value: '30%'},
          {lable: '40', value: '40%'},
          {lable: '50', value: '50%'},
          {lable: '60', value: '60%'},
          {lable: '70', value: '70%'},
          {lable: '80', value: '80%'},
          {lable: '90', value: '90%'},
          {lable: '100', value: '100%'}
        ],
        addSign: {
          show: false,
          all: false,
          parallel: 'false',
          allStrage: 'false',
          doUserId: '',
          pass: 'next',
          nextId: '',
          signScale: '',
          signType: 'scale',
          unPass: 'next',
          userTaskName: '',
          usersInfo: []

        },
        sequence: {
          users: []
        },
        addSignRuleValidate: {
          nextId: [
            {required: true, message: '目标节点必须填写', trigger: 'blur'}
          ],
          signType: [
            {required: true, message: '策略必填', trigger: 'blur'}
          ],
          signScale: [
            {required: true, message: '比例必填', trigger: 'blur'}
          ],
          userTaskName: [
            {required: true, message: '任务名称必填', trigger: 'blur'}
          ]
        },
        informed: {
          show: false,
          informed: false,
          usersInfo: [],
          opinion: '',
          taskTitle: '',
          taskId: ''

        },
        agreeAdHocSubProcess: {
          show: false,
          usersIds: [],
          userNames: [],
          nextNode: [],
          nextNodeName: [],
          nextNodeNameOptions: [],
          taskId: "",
          usersInfo: [],
          nodeIndex: 0,
          priority: '50',
          taskTitle: '',
          option: '',
          chooseNode: '',
          chooseNodeUser: '',
        },
        backToNodeForm: {
          show: false,
          usersInfo: [],
          option: '',
          taskTitle: '',
          taskId: '',
          priority: '50',
          newActivityId: '',
          operate: 'backToNode',
          nodeArray: []
        },
        backToNodFormRuleValidate: {
          newActivityId: [
            {required: true, message: '退回节点必须填写', trigger: 'blur'}
          ],
          option: [
            {required: true, message: '意见必须填写', trigger: 'blur'}
          ]
        },
        scopeType: "",
        monitor: false,

        tmpTaskInfo: {taskTitle: '',},
        task: {
          taskName: '',
          taskId: '',
          opinion: '',
          formType: 'inner',
          actionName: '',
          action: '',
          nodeType: '',
          usersIds: [],
          userNames: [],
          nextNode: [],
          chooseNode: '',
          chooseNodeUser: '',
          formData: '',
          nodeIndex: 0,
          priority: '50',
          taskTitle: ''

        },
        ruleValidate: {
          opinion: [
            {required: true, message: '意见必须填写', trigger: 'blur'}
          ]
        },
        taskSequenceRuleValidate: {
          opinion: [
            {required: true, message: '意见必须填写', trigger: 'blur'}
          ]
        },
        backToNodeRuleValidate: {
          opinion: [
            {required: true, message: '意见必须填写', trigger: 'blur'}
          ]
        },

        informedRuleValidate: {
          taskTitle: [
            {required: true, message: '标题必须填写', trigger: 'blur'}
          ],
          opinion: [
            {required: true, message: '意见必须填写', trigger: 'blur'}
          ]

        },
        transferTaskRuleValidate: {
          usersInfo: [
            {required: true, message: '转办人必须填写', trigger: 'blur'}
          ],
          option: [
            {required: true, message: '意见必须填写', trigger: 'blur'}
          ]

        },
        transactionUrgeRuleValidate: {
          newActivityId: [
            {required: true, message: '节点必须填写', trigger: 'blur'}
          ],
          usersInfo: [
            {required: true, message: '人员必须填写', trigger: 'blur'}
          ],
          option: [
            {required: true, message: '意见必须填写', trigger: 'blur'}
          ]

        },
        addNodeRuleValidate: {
          userTaskName: [
            {required: true, message: '任务名称必填', trigger: 'blur'}
          ],
          nextId: [
            {required: true, message: '目标节点必填', trigger: 'blur'}
          ]

        }
      }
    },
    mounted () {
      window.addEventListener('message', this.handleMessage)
      this.iframeWin = this.$refs.iframe.contentWindow

    },

    created() {
      var query = this.$route.query
      var taskInfo = query.taskInfo;
      if (this.taskInfo) {
        this.taskId = this.taskInfo
        this.task.taskId = this.taskInfo
        this.fromIndex = true;
        this.getBiz(this.taskInfo);
      } else {
        this.fromIndex = false;
      }

    },
    props: {
      taskInfo: {
        type: String,
        default: ""
      }
    },
    watch: {
      taskInfo() {
        if (this.taskInfo) {
          this.taskId = this.taskInfo
          this.task.taskId = this.taskInfo
          this.fromIndex = true;
          this.getBiz(this.taskInfo);
        } else {
          this.fromIndex = false;
        }
      }
    },

    methods: {
      handleMessage (event) {
        // 根据上面制定的结构来解析iframe内部发回来的数据
        const data = event.data
        switch (data.cmd) {
          case 'close':
            this.iframeWin.postMessage({
              cmd: 'clear',
              params: {}
            }, '*')
            this.go()
            break
        }
      },
      restartShow: function () {
        this.handlerModel = false
        this.agreeAdHocSubProcess.show = false
        this.headMap.show = false
        this.opinion.show = false
        this.flowImage.show = false
        this.backToNodeForm.show = false
        this.transactionUrge.show = false
        this.addNode.show = false
        this.addSign.show = false
      },
      initData: function () {
      },
      triggerRuleValidate: function () {

      },
      handleCheckedNextNodeChange: function (data) {
      },
      freedomChoice: function () {
        getTransactionUrge(this.taskId).then((response) => {
          if (response.code == 0) {
            this.backToNodeForm.nodeArray = response.data
          }
        })
      },
      backToNodeChange: function (nodeId) {
        getBackNodeUser(this.taskId, nodeId).then((response) => {
          if (response.code == 0) {
            if (response.data && response.data.length > 0) {
              if (response.data[0].assigneeName && response.data[0].assignee) {
                var assigneeName = response.data[0].assigneeName
                var assignee = response.data[0].assignee
                this.singleUserData = {'userNo': assignee, 'fullname': assigneeName}
                this.backToNodeForm.usersInfo = this.singleUserData
              } else {
                this.singleUserData = {}
                this.backToNodeForm.usersInfo = {}
              }
            } else {
              this.singleUserData = {}
              this.backToNodeForm.usersInfo = {}
            }
          }
        })
      },
      confirmDialogEvent: function (data) {
        if (data) {
          switch (this.alias) {
            case 'agree':// 普通任务正常办理

              this.task.nextNode[this.task.nodeIndex].extObj = data
              break
            case 'informed':
              this.informed.usersInfo = data
              this.existData = data
              break
            case 'transferTask':
              this.transferTask.usersInfo = data
              this.existData = data
              break
            case 'transactionUrge':
              this.transactionUrge.usersInfo = data
              this.existData = data
              break
            case 'agreeAdHocSubProcess':

              this.agreeAdHocSubProcess.nextNode[this.task.nodeIndex].extObj = data
              break
            case 'addSignNode':
              this.addSign.usersInfo = data
              this.existData = data
              break
            case 'sequence':
              this.sequence.users = data
              this.sequenceNode[0].extObj = data
              this.taskSequence.userInfo = data;
              break
            default:
          }
        }
      },
      confirmDialogSingleEvent: function (data) {
        if (data) {
          this.singleUserData = data
        }
      },
      confirmInformedDialogEvent: function (data) {
        if (data) {
        }
      },

      selectUsers: function (index) {
        this.task.nodeIndex = index
        var users = this.task.nextNode[index].extObj
        this.existData = users
        this.show = true

      },
      selectSequenceUsers: function (index) {
        var users = this.sequenceNode[0].extObj;
        this.existData = []
        if (users) {
          this.existData = users
        }
        this.show = true

      },
      selectAddSignUsers: function () {
        try {
          this.existData = this.addSign.usersInfo;

        } catch (e) {
          this.existData = []
        }
        this.show = true
      },
      selectInformedUsers: function () {
        try {
          this.existData = this.informed.usersInfo;

        } catch (e) {
          this.existData = []
        }
        this.show = true
      },
      selectAgreeAdHocSubProcessUsers: function (index) {
        this.task.nodeIndex = index;
        try {
          this.existData = this.agreeAdHocSubProcess.nextNode[index].extObj

        } catch (e) {
          this.existData = []
        }
        this.show = true
      },
      selectSingleShowUsers: function () {
        this.singleShow = true
      },
      delUsers: function (index, userIndex) {
        this.task.nextNode[index].extObj.splice(userIndex, 1)
      },
      delSequenceUsers: function (index, userIndex) {
        this.sequenceNode[0].extObj.splice(userIndex, 1)
      },
      delCommonUsers: function (index) {
        this.informed.usersInfo.splice(index, 1)
        this.addSign.usersInfo.splice(index, 1)
        this.existData.splice(index, 1)
      },
      delSingleUsers: function (index) {
        this.singleUserData = {}
      },
      getFormData: function () {
        if (this.$refs.generateForm) {
          this.$refs.generateForm.getData().then(data => {
            this.task.formData = JSON.stringify(data)
            return data
          }).catch(e => {
          })
        }

      },
      backToNodeHandler: function () {
        new Promise((resolve, reject) => {
          if (this.singleUserData && this.singleUserData.userNo) {
            this.$refs['backToNodeForm'].validate((valid) => {
              if (valid) {
                const obj = JSON.parse(JSON.stringify(this.backToNodeForm))
                delete (obj['show'])
                delete (obj['nodeArray'])
                delete (obj['usersInfo'])
                obj['userId'] = this.singleUserData.userNo
                obj['userName'] = this.singleUserData.fullname
                obj['taskId'] = this.taskId
                delete (obj['usersInfo'])
                this.loading = true
                changeActivity(obj).then((response) => {
                  if (response.code == 0) {
                    this.go()
                  } else {
                    this.$message({"duration": '800', message: response.msg, type: 'warning', customClass: 'zZindex'});

                  }
                  this.loading = false
                })
              } else {

              }
            })
          } else {
            that.$message({message: '请选择处理人', type: 'warning', customClass: 'zZindex'});
          }
        })
      },
      transactionUrgeHandler: function () {
        new Promise((resolve, reject) => {
          if (this.singleUserData && this.singleUserData.userNo) {
            this.$refs['transactionUrge'].validate((valid) => {
              if (valid) {
                this.changeActivityAction()
              } else {

              }
            })
          } else {
            this.$message({"duration": '800', message: '请选择处理人', type: 'warning', customClass: 'zZindex'});

          }
        })
      },
      afterAddSignHandler: function () {
        this.addSign.all = false
        this.addSign.parallel = 'false'
        this.addSign.allStrage = 'false'
        this.addSign.doUserId = ''
        this.addSign.pass = 'next'
        this.addSign.nextId = ''
        this.addSign.signScale = ''
        this.addSign.signType = 'scale'
        this.addSign.unPass = 'next'
        this.addSign.userTaskName = ''
        this.addSign.usersInfo = []
      },
      addSignHandler: function () {
        this.loading = false;
        new Promise((resolve, reject) => {
          if (this.addSign.usersInfo && this.addSign.usersInfo.length > 0) {
            this.$refs['addSign'].validate((valid) => {
              if (valid) {
                const obj = JSON.parse(JSON.stringify(this.addSign))
                delete (obj['show'])
                obj['taskId'] = this.taskId
                this.loading = true;
                addSignOperate(obj).then((response) => {
                  if (response.code == 0) {
                    this.$message({"duration": '800', message: '操作成功', type: 'success', customClass: 'zZindex'});
                    this.addSign.show = false
                    this.afterAddSignHandler()
                    this.go();
                  } else {
                    this.$message({"duration": '800', message: response.msg, type: 'success', customClass: 'zZindex'});

                  }
                  this.loading = false;
                })
              } else {

              }
            })
          } else {
            this.$message({"duration": '800', message: '请选择处理人', type: 'warning', customClass: 'zZindex'});
          }
        })
      },
      addNodeHandler: function () {
        try {
          this.loading = false;

          new Promise((resolve, reject) => {
            if (this.singleUserData && this.singleUserData.userNo) {
              this.$refs['addNode'].validate((valid) => {
                if (valid) {
                  // addNodeOperate
                  const obj = JSON.parse(JSON.stringify(this.addNode))
                  delete (obj['show'])
                  obj['taskId'] = this.taskId
                  obj['users'] = this.singleUserData.userNo
                  this.loading = true;
                  addNodeOperate(obj).then((response) => {
                    if (response.code == 0) {
                      this.$message({"duration": '800', message: '操作成功', type: 'success', customClass: 'zZindex'});
                      this.addNode.show = false
                      this.go();
                    } else {
                      this.$message({
                        "duration": '800',
                        message: response.msg,
                        type: 'warning',
                        customClass: 'zZindex'
                      });
                    }
                    this.loading = false;
                  })
                } else {

                }
              })
            } else {
              this.$message({"duration": '800', message: '请选择处理人', type: 'warning', customClass: 'zZindex'});

            }
          })
        } finally {
          this.loading = false;

        }

      },
      changeActivityAction: function () {
        this.loading = false;
        const obj = JSON.parse(JSON.stringify(this.transactionUrge))
        delete (obj['show'])
        obj['userId'] = this.singleUserData.userNo
        obj['userName'] = this.singleUserData.fullname
        obj['taskId'] = this.taskId
        delete (obj['usersInfo'])
        this.loading = true;
        changeActivity(obj).then((response) => {
          if (response.code == 0) {
            this.go()
          } else {
            this.$message({"duration": '800', message: response.msg, type: 'warning', customClass: 'zZindex'});

          }
          this.loading = false;
        })
      },
      completeSequenceTask: function () {
        this.loading = false;
        new Promise((resolve, reject) => {
          if (this.sequenceNode[0]) {
            this.taskSequenceForm.chooseNode = this.sequenceNode[0].chooseNode
            this.taskSequenceForm.chooseNodeUser = this.sequenceNode[0].chooseNodeUser
          }

          this.taskSequenceForm.taskId = this.task.taskId;
          this.taskSequenceForm.formData = this.task.formData
          this.loading = true;
          completeTask(this.taskSequenceForm).then((response) => {
            if (response.code == 0) {
              this.handlerSequenceModel = false;
              this.go()
            } else {
              this.$message({"duration": '800', message: response.msg, type: 'warning', customClass: 'zZindex'});

            }
            this.loading = false;

          })
        })
      },

      agreeTaskSequenceHandler: function () {
        //taskSequence
        this.loading = false;
        this.$refs['taskSequenceForm'].validate((valid) => {
          if (valid) {
            var nextNodeStr = ''
            var nextNodeStrUser = ''
            for (var i = 0; i < this.sequenceNode.length; i++) {
              nextNodeStr += this.sequenceNode[i].taskId
              if (i < this.sequenceNode.length - 1) {
                nextNodeStr += ','
              }

              if (this.sequenceNode[i].extObj) {
                for (var j = 0; j < this.sequenceNode[i].extObj.length; j++) {
                  nextNodeStrUser += this.sequenceNode[i].extObj[j].userNo
                  if (j < this.sequenceNode[i].extObj.length - 1) {
                    nextNodeStrUser += ','
                  }
                }
                if (this.sequenceNode.length > 1 && i < this.sequenceNode.length - 1) {
                  nextNodeStrUser += '_'
                }
              }
            }
            if (this.sequenceNode && this.sequenceNode[0]) {
              this.sequenceNode[0].chooseNode = nextNodeStr
              this.sequenceNode[0].chooseNodeUser = nextNodeStrUser
            }
            const that = this
            setTimeout(function () {
              that.completeSequenceTask()
            }, 300)
          } else {
            this.$message({"duration": '800', message: '表单校验不通过', type: 'warning', customClass: 'zZindex'});

          }
        })
      },
      agreeAdHocSubProcessHandle: function () {
        this.loading = false;
        this.$refs['agreeAdHocSubProcess'].validate((valid) => {
          var nextNodeName = this.agreeAdHocSubProcess.nextNodeName
          if (valid) {
            var nextNodeStr = ''
            var nextNodeStrUser = ''
            for (var i = 0; i < this.nextNode.length; i++) {
              var taskName = this.nextNode[i].taskName
              var flag = false;
              for (var j = 0; j < nextNodeName.length; j++) {
                if (nextNodeName[j] == taskName) {
                  flag = true;
                  break
                }
              }
              ;


              if (flag) {
                nextNodeStr += this.nextNode[i].taskId
                if (i < this.nextNode.length - 1) {
                  nextNodeStr += ','
                }

                if (this.nextNode[i].extObj) {
                  for (var j = 0; j < this.nextNode[i].extObj.length; j++) {
                    nextNodeStrUser += this.nextNode[i].extObj[j].userNo
                    if (j < this.nextNode[i].extObj.length - 1) {
                      nextNodeStrUser += ','
                    }
                  }
                  if (this.nextNode.length > 1 && i < this.nextNode.length - 1) {
                    nextNodeStrUser += '_'
                  }
                }
              }
            }

            var userNodeReg = /,$/gi;//
            var userReg = /_$/gi;//
            nextNodeStrUser = nextNodeStrUser.replace(userReg, "");
            nextNodeStr = nextNodeStr.replace(userNodeReg, "");
            this.agreeAdHocSubProcess.chooseNode = nextNodeStr.replace(userReg, "");
            this.agreeAdHocSubProcess.chooseNodeUser = nextNodeStrUser
            const that = this
            setTimeout(function () {
              that.completeAdHoc()
            }, 300)
          } else {
          }
        })
      },
      agreeHandler: function () {
        this.loading = false;
        this.$refs['task'].validate((valid) => {
          if (valid) {
            var nextNodeStr = ''
            var nextNodeStrUser = ''
            for (var i = 0; i < this.nextNode.length; i++) {
              nextNodeStr += this.nextNode[i].taskId
              if (i < this.nextNode.length - 1) {
                nextNodeStr += ','
              }

              if (this.nextNode[i].extObj) {
                for (var j = 0; j < this.nextNode[i].extObj.length; j++) {
                  nextNodeStrUser += this.nextNode[i].extObj[j].userNo
                  if (j < this.nextNode[i].extObj.length - 1) {
                    nextNodeStrUser += ','
                  }
                }
                if (this.nextNode.length > 1 && i < this.nextNode.length - 1) {
                  nextNodeStrUser += '_'
                }
              }
            }
            this.task.chooseNode = nextNodeStr
            this.task.chooseNodeUser = nextNodeStrUser
            const that = this
            this.loading = true;
            setTimeout(function () {

              that.completeTask();
              that.loading = false;
            }, 300)
          } else {
            this.handlerModel = true
          }
        })
      },
      informedTask: function () {
        this.loading = false;
        new Promise((resolve, reject) => {
          this.informed.taskId = this.taskId
          this.$refs['informed'].validate((valid) => {
            if (valid) {
              this.informedTaskAction()
            } else {

            }
          })
        })
      },
      transferTaskAction: function () {
        this.loading = false;
        new Promise((resolve, reject) => {
          this.transferTask.taskId = this.taskId
          if (this.singleUserData && this.singleUserData.userNo) {
            this.$refs['transferTask'].validate((valid) => {
              if (valid) {
                this.doTransferTaskAction()
              } else {

              }
            })
          } else {
            this.$message({"duration": '800', message: '请选择转办人', type: 'warning', customClass: 'zZindex'});

          }
        })
      },
      afterInformedTaskAction: function () {
        this.informed.informed = false
        this.informed.opinion = ''
        this.informedReset()
        this.$message({"duration": '800', message: '传阅成功', type: 'success', customClass: 'zZindex'});
        this.$refs.screenfull.getRunCommuListCount();
      },
      afterTransferTaskAction: function () {
        this.go()
      },
      informedTaskAction: function () {
        this.loading = false;
        const obj = JSON.parse(JSON.stringify(this.informed))
        delete (obj['show'])
        delete (obj['informed'])
        this.loading = true;
        informedTask(obj).then((response) => {
          if (response.code == 0) {

            this.afterInformedTaskAction()
          } else {
            this.$message({"duration": '800', message: response.msg, type: 'warning', customClass: 'zZindex'});

          }
          this.loading = false;
        })
      },
      doTransferTaskAction: function () {
        this.loading = false;
        const obj = JSON.parse(JSON.stringify(this.transferTask))
        obj['usersInfo'] = this.singleUserData
        delete (obj['show'])
        delete (obj['model'])
        transferTask(obj).then((response) => {
          if (response.code == 0) {
            this.$message({"duration": '800', message: '转办成功', type: 'success', customClass: 'zZindex'});
            this.transferTask.model = false
            const that = this
            this.loading = true;
            setTimeout(function () {
              that.afterTransferTaskAction()
              that.loading = false;
            }, 300)
          } else {
            this.$message({"duration": '800', message: response.msg, type: 'warning', customClass: 'zZindex'});


          }
        })
      },
      completeTask: function () {
        this.loading = false;
        new Promise((resolve, reject) => {
          const objB = JSON.parse(JSON.stringify(this.task))
          objB['monitor'] = this.monitor
          delete (objB['usersIds'])
          delete (objB['userNames'])
          delete (objB['nextNode'])

          completeTask(objB).then((response) => {
            if (response.code == 0) {
              this.go()
            } else {
              this.$message({"duration": '800', message: response.msg, type: 'success', customClass: 'zZindex'});

            }
          })
        })
      },
      /**
       * 完成AdHoc任务
       */
      completeAdHoc: function () {
        this.loading = false;
        new Promise((resolve, reject) => {
          const objB = JSON.parse(JSON.stringify(this.agreeAdHocSubProcess))
          delete (objB['usersIds'])
          delete (objB['userNames'])
          delete (objB['nextNode'])
          objB["formData"] = this.task.formData
          objB["taskId"] = this.task.taskId
          this.loading = true
          completeAdHoc(objB).then((response) => {
            if (response.code == 0) {
              this.go()
            } else {
              this.$message({"duration": '800', message: response.msg, type: 'success', customClass: 'zZindex'});

            }
            this.loading = false
          })
        })
      },
      informedReset() {
        this.informed.informed = false
        this.existData = []
        this.informed.usersInfo = []
      },
      handleReset(name) {
        this.handlerModel = false
        this.informed.informed = false
        this.informed.opinion = ''
        this.informedReset()
        this.loading = false;
        if (name == 'task') {
          this.task.opinion = ""
          this.task.priority = '50'
          this.task.taskTitle = JSON.parse(JSON.stringify(this.tmpTaskInfo)).taskTitle
        }
        if (name == 'transferTask') {
          this.handleTransferTaskReset()
        }
        if (name == 'addNode') {
          this.handleAddNodeReset()
        }
        if (name == 'transactionUrge') {
          this.handleTransactionUrgeReset()
        }
        if (name == 'addSign') {
          this.handleAddSignReset()
        }
        if (name == 'agreeAdHocSubProcess') {
          this.agreeAdHocSubProcess.show = false;
        }
        if (name == 'backToNode') {
          this.handleBackToNodeReset()
        }
        if (name == 'taskSequence') {
          this.handlerSequenceModel = false
        }
      },
      handleTransferTaskReset() {
        this.transferTask.usersInfo = {}
        this.singleUserData = {}
        this.transferTask.option = ''
        this.transferTask.model = false
      },
      handleBackToNodeReset() {
        this.backToNodeForm.usersInfo = {}
        this.backToNodeForm.show = false
        this.backToNodeForm.option = ''
        this.backToNodeForm.newActivityId = ''
        this.singleUserData = {}
      },
      handleTransactionUrgeReset() {
        this.transactionUrge.usersInfo = {}
        this.singleUserData = {}
        this.transactionUrge.newActivityId = ''
        this.transactionUrge.option = ''
        this.transactionUrge.show = false
      },
      handleAddNodeReset() {
        this.singleUserData = {}
        this.addNode.nextId = ''
        this.addNode.userTaskName = ''
        this.addNode.show = false
      },
      handleAddSignReset() {
        this.addSign.usersInfo = []
        this.addSign.userTaskName = ''
        this.addSign.nextId = ''
        this.addSign.doUserId = ''
        this.addSign.show = false
        this.existData = []
      },
      informedHandler() {
        this.informedTask()
      },
      transferTaskHandler() {
        this.transferTaskAction()
      },

      action: function (action, type) {
        if (typeof action === 'object') {
          this.alias = action.alias
        } else {
          this.alias = action
        }
        if (type) {
          this.getFormData()
          this.alias = "sequence";
          const obj = JSON.parse(JSON.stringify(this.nextNode))
          this.taskSequenceForm.taskTitle = this.task.taskTitle;

          var sequenceNode = [];
          obj.forEach((item, index, array) => {
            //执行代码
            if (item.taskId == action.targetRef) {
              sequenceNode.push(item);

            }
          })
          this.sequenceNode = sequenceNode;

          this.handlerSequenceModel = true;
        } else {
          switch (this.alias) {
            case 'agree':// 普通任务正常办理

              this.task.action = 'agree'
              this.task.actionName = 'agree'
              this.task.formType = 'inner'
              this.getFormData()
              this.handlerModel = true
              break
            case 'pass':// 多实例通过

              this.task.action = 'pass'
              this.task.actionName = 'pass'
              this.task.formType = 'inner'
              this.getFormData()
              this.handlerModel = true
              break
            case 'unPass':// 多实例不通过

              this.task.action = 'unPass'
              this.task.actionName = 'unPass'
              this.task.formType = 'inner'
              this.getFormData()
              this.handlerModel = true
              break
            case 'informed':
              this.informed.informed = true
              break
            case 'agreeAdHocSubProcess':
              this.getFormData()
              this.agreeAdHocSubProcess.show = true;
              break
            case 'transferTask':
              this.transferTask.model = true
              break
            case 'invalid':
              this.invalidTaskAction()
              break
            case 'headmap':
              this.headMapAction()
              break
            case 'flowImage':
              this.flowImageAction()
              break
            case 'approvalHistory':
              this.opinionAction()
              break
            case 'transactionUrge':

              this.transactionUrgeAction()
              break
            case 'addNode':
              this.addNodeAction()
              break
            case 'addSignNode':

              this.addSignNode()
              break
            case 'saveDraft':
              this.saveAction()
              break
            case 'backToNode':

              this.backToNodeAction()
              break
            case 'print':

              window.print()
              break
            default:

              this.task.action = 'agree'
              this.task.actionName = 'agree'
              this.task.formType = 'inner'
              this.getFormData()
              this.handlerModel = true
          }
        }


      },
      backToNodeAction: function () {
        getBackNode(this.taskId).then((response) => {
          if (response.code == 0) {
            this.backToNodeForm.nodeArray = response.data
          }
          this.backToNodeForm.show = true
        })
        this.backToNodeForm.taskTitle = this.task.taskTitle
        // getBackNode
      },
      saveAction: function () {
        var obj = {}
        obj['taskId'] = this.taskId
        const that = this
        this.getFormData()
        setTimeout(function () {
          var formData = that.task.formData
          obj['formData'] = formData
          saveform(obj).then((response) => {
            if (response.code == 0) {
              that.$message({"duration": '800', message: '保存成功', type: 'success', customClass: 'zZindex'});


            }
          })
        }, 300)
      },
      addNodeAction: function () {
        this.handleReset('addNode')
        getOutgoingFlows(this.taskId).then((response) => {
          if (response.code == 0) {
            this.transactionUrgeData.nodeArray = response.data
            this.transactionUrge.taskTitle = this.task.taskTitle
            this.addNode.show = true

          }
        })

      },
      addSignNode: function () {
        this.handleAddSignReset()
        getOutgoingFlows(this.taskId).then((response) => {
          if (response.code == 0) {
            this.transactionUrgeData.nodeArray = response.data
            this.transactionUrge.taskTitle = this.task.taskTitle
            this.addSign.show = true
          }
        })
      },
      transactionUrgeAction: function () {
        getTransactionUrge(this.taskId).then((response) => {
          if (response.code == 0) {
            this.transactionUrgeData.nodeArray = response.data
            this.transactionUrge.taskTitle = this.task.taskTitle
            this.transactionUrge.show = true
          }
        })
      },
      opinionAction: function () {
        this.opinion.show = true
        opinionTask(this.processInstanceId).then((response) => {
          if (response.code == 0) {
            this.opinion.tableData = response.data
          }
        })
      },
      headMapAction: function () {
        var url = this.headMap.base + this.processDefinitionId + '&t=' + new Date().getTime();
        this.headMap.src = url
        this.headMap.show = true
      },
      flowImageAction: function () {
        var url = this.flowImage.base + this.processInstanceId + '&t=' + new Date().getTime();
        this.flowImage.src = url;
        this.flowImage.show = true
      },
      invalidTaskAction: function () {
        invalidTask(this.taskId).then((response) => {
          if (response.code == 0) {
            this.$message({"duration": '800', message: '作废成功', type: 'success', customClass: 'zZindex'});

            const that = this
            setTimeout(function () {
              that.go()
            }, 300)
          }
        })
      },
      go: function () {
        this.handlerModel = false
        // 调用全局挂载的方法
        if (this.fromIndex) {
          //首页过来的
          this.jsonData = {}
          this.editData = {}
          this.fromIndex = false;
          try {
            this.$emit('closeEvent', true)

          }catch (e) {

          }
        } else {
        }
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.push({name: 'myTask', query: {t: new Date().getTime()}})

      },
      getBiz: function (taskId) {
        getBiz(taskId).then((response) => {
          if (response.code == 0) {

            this.bizKey = response.businessKey
          var editUrl=  response.data.editUrl+"&messageId="+getToken() + "&t=" + new Date().getTime();
            this.editSrc=editUrl
           // this.editSrc = process.env.VUE_APP_BASE_BPM_API + 'oa/desform/edit/oa_promotion/' + this.bizKey+"?taskId="+taskId
            this.processInstanceId = response.processInstanceId;
            this.taskTitle=response.taskTitle
            this.isSpinShow=false
          } else {
          }
        })
      },

    }

  }

</script>
<style lang="scss" scoped>
  .task-card {
    margin-top: 10px;
  }

  .container {
    height: auto !important;
    min-height: 450px;
  }

  .zZindex {
    z-index: 3000 !important;
  }

</style>
